/**
 * Created by jt on 2017/5/6.
 */
import "./index.less";
var Login = function () {

    return {
        //main function to initiate the module
        init: function () {

            $('.login-form').validate({
                errorElement: 'label', //default input error message container
                errorClass: 'help-inline', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                rules: {
                    username: {
                        required: true
                    },
                    password: {
                        required: true
                    },
                    remember: {
                        required: false
                    }
                },

                messages: {
                    username: {
                        required: "请填写用户名"
                    },
                    password: {
                        required: "请填写密码"
                    }
                },

                invalidHandler: function (event, validator) { //display error alert on form submit
                    $('.alert-error', $('.login-form')).show();
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.control-group').addClass('error'); // set error class to the control group
                },

                success: function (label) {
                    label.closest('.control-group').removeClass('error');
                    label.remove();
                },

                errorPlacement: function (error, element) {
                    error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
                },

                submitHandler: function (form) {
                    let $form = $(form);
                    let username = $form.find('.username')[0].value;
                    let password = $form.find('.password')[0].value;
                    let data = {
                        "yonhum": username,
                        "mim": password,
                        "userType":"ums"
                    };
                    jQuery.ajax({
                        url: HOST_ADDR+'/cpmp/accessToken',
                        type: 'post',
                        data: JSON.stringify(data),
                        dataType: 'json',
                        contentType:"application/json",
                        success: function(resp){
                            if(resp.access_token){
                                sessionStorage.setItem('token',resp.access_token);
                                sessionStorage.setItem('reseIndex',resp.role_code);
                                sessionStorage.setItem('username', username);
                                sessionStorage.setItem('realName', resp.userinfo.real_name);
                                if( typeof(resp.userinfo.user_info.mobilephone)!='undefined' && resp.userinfo.user_info.mobilephone !=null){
                                    sessionStorage.setItem('mobilephone', resp.userinfo.user_info.mobilephone);
                                }
                                sessionStorage.setItem('orgId', resp.userinfo.company.id);
                                sessionStorage.setItem('ctct', resp.userinfo.company.ctct);
                                sessionStorage.setItem('ctctTel', resp.userinfo.company.ctctTel);
                                sessionStorage.setItem('bidDept', resp.userinfo.company.bidDept);
                                sessionStorage.setItem('bidDeptAddr', resp.userinfo.company.bidDeptAddr);
                                sessionStorage.setItem('manageCompanies', JSON.stringify(resp.userinfo.manageCompanies));
                                if(resp.userinfo.roles.length && resp.userinfo.roles[0].role_name){
                                    sessionStorage.setItem('roleName',resp.userinfo.roles[0].role_name);
                                }
                                console.log(resp);
                                location.href='/portal/home.html';

                            } else if (resp.error) {
                                alert('账号或密码错误');
                                window.location.reload();
                            } else {
                                window.location.reload();
                            }

                        },
                        error:function(err){
                            alert('账号或密码错误');
                        }

                    })
                }
            });

            $('.login-form input').keypress(function (e) {
                if (e.which == 13) {
                    if ($('.login-form').validate().form()) {
                        // window.location.href = "index.html";
                    }
                    return false;
                }
            });

        }

    };

}();

jQuery(document).ready(function() {

    App.init();

    Login.init();

});